home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 398 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: library.erc.clarkson.edu!rpi!not-for-mail
  2. From: thorstad@dg-rtp.dg.com (Brian Thorstad)
  3. Newsgroups: comp.lang.c++.moderated,comp.lang.c++
  4. Subject: Re: Meaning of the specifier volatile?
  5. Date: 4 Jan 1996 00:26:02 -0000
  6. Organization: Data General Corporation, RTP, NC
  7. Sender: cppmods@netlab.cs.rpi.edu
  8. Approved: herbs@connobj.com
  9. Message-ID: <4cf6qq$g0e@netlab.cs.rpi.edu>
  10. References: <4c9740$27n@netlab.cs.rpi.edu> <4cb5i3$5mr@netlab.cs.rpi.edu>
  11. NNTP-Posting-Host: netlab.cs.rpi.edu
  12.  
  13. X-Original-Date: 03 Jan 1996 14:10:13 GMT
  14.  
  15. In article <4cb5i3$5mr@netlab.cs.rpi.edu> Dave Nulton <dnult@axiom.net> writes:
  16.  
  17.    I'm no expert but here is what I know.  The specifier volatile is 
  18.    a directive to the compiler that the variable declared volatile 
  19.    can change at any moment.  The compiler will then make no 
  20.    assumptions about the contents of this variable, and will always 
  21.    re-check its value when used.  An example of something that might 
  22.    use volatile variables is:  a hardware interface to your PC which 
  23.    reads analog data and converts it to digital.  Your program may 
  24.    read the data from the PC bus.  However this data is always 
  25.    changing and your program should make know assumptions that the 
  26.    data is the same - it should always recheck the value.
  27.  
  28. I believe volatile directs the compiler to not optimize the variable
  29. as the value can change w/o the compiler being aware.  The compiler
  30. will not "always recheck the value", but will do exactly what you
  31. coded -- nothing more, nothing less.  It is up to the programmer to
  32. appropriately access this value.  A classic example is accessing such
  33. a value inside a loop, where normal optimization might pull that
  34. access outside the loop.  Volatile can prevent this.
  35.  
  36. >From the ARM:  "volatile is a hint to the compiler to avoid aggressive
  37. optimization involving the object because the value of the object may
  38. be changed by means undetectable by a compiler."
  39.  
  40. My $0.02.
  41. Brian
  42.  
  43.    I'm sure there are other less complicated applications which may 
  44.    benefit from volatile variables.  I'm sure one of the many other 
  45.    experts on the net can elaborate.
  46.  
  47.    -dnult@axiom.net
  48. -- 
  49.  
  50. +---------------------------+----------------------------------+
  51. | Brian Thorstad            | Data General Corporation         |
  52. |  (thorstad@dg-rtp.dg.com) |                                  |
  53. | Voice: (919) 248-6062     | 62 T.W. Alexander Dr.            |
  54. | Fax:   (919) 248-6108     | Research Triangle Park, NC 27709 |
  55. +---------------------------+----------------------------------+
  56.  
  57.     [ comp.lang.c++.moderated is a moderated newsgroup.  Submit articles ]
  58.     [  to <c++-submit@netlab.cs.rpi.edu>.  The moderation policy can be  ]
  59.     [   retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>.   ]
  60.     [    Moderators can be reached at: c++-request@netlab.cs.rpi.edu.    ]
  61.